//
//  Extension+UIImage.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit


extension UIImage{
    
    
    // MARK: - 自定义键盘表情
    class func emoticonWithImage(emoticon:WBEmoticonModel) -> UIImage?{
    
        let path = emoticon.path ?? ""
        let png = emoticon.png ?? ""
        //不是emoji图片
        let image = UIImage(named: "\(path)\(png)", inBundle: WBEmoticonTool.sharedEmoticonTool.emoticonBundle, compatibleWithTraitCollection: nil)
    
        return image
    
    }
    
    
    
    // MARK: - 上传图片压缩
    class func handleImageSize(image:UIImage , width: CGFloat) -> UIImage{
        
        if image.size.width < width{
            
            return image
        }
        
        //计算等比例后的高度
        let height = image.size.height * width / image.size.width
        
        //计算rect
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        //压缩图片
        //开启上下文
        UIGraphicsBeginImageContext(rect.size)
        
        //渲染图片
        image.drawInRect(rect)
        //取出图片
        let result = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return result
    }

    


}